#pragma once
#include<vector>
#include<fstream>
using namespace std;
//矩形类
//rectangle rt_collection[1000];
class rectangle {
protected:
    rectangle(){}
    void delayConstru(int x1, int y1, int x2, int y2)
    {
        width = x2 - x1;
        height = y2 - y1;

        area=width*height;

        x_mid = (x1 + x2) / 2;
        y_mid = (y2 + y1) / 2;

        up = false;
        right = false;

        this->x1 = x1;
        this->y1 = y1;
        this->x2 = x2;
        this->y2 = y2;

        this->init_x1=x1;
        this->init_y1=y1;
        this->init_x2=x2;
        this->init_y2=y2;
    }

public:

    bool operator ==(const rectangle& p) const{
        if(p.x1==this->x1&&p.y1==this->y1&&p.x2==this->x2&&p.y2==this->y2){
            return true;
        }
        return false;
    }
    //矩形编号，和信息素表的顺序相同,主函数初始化的时候获得
    int index;

    //长宽
    double width;
    double height;
    //面积
    double area;
    //位置信息
    int x1;
    int y1;
    int x2;
    int y2;
    int x_mid;//矩形中心点坐标
    int y_mid;

    //原始位置信息
    int init_x1;
    int init_x2;
    int init_y1;
    int init_y2;



    //上右摆放状态
    bool up;
    bool right;

    //记录该矩形是否被摆放
    bool flag=false;//否


    //该矩形的左右孩子节点，左节点对应上，右节点对应右
    rectangle *r_left=NULL;
    rectangle *r_right=NULL;

    rectangle(int x1, int y1, int x2, int y2)
    {
        this->delayConstru(x1,y1,x2,y2);
    }

    //进入槽子，被拼到当前矩形上部，修改信息
    void set_up(rectangle *r_now)
    {
        this->x1 = (*r_now).x1;
        this->y1 = (*r_now).y1 + (*r_now).height;
        this->x2 = this->x1 + this->width;
        this->y2 = this->y1 + this->height;

        this->x_mid = this->x1 + (this->width) / 2;
        this->y_mid = this->y1 + (this->height) / 2;
    }

    //进入槽子，被拼到当前矩形右部，修改信息
    void set_right(rectangle *r_now)
    {

        this->x1 = (*r_now).x1 + (*r_now).width;
        this->y1 = (*r_now).y1;
        this->x2 = this->x1 + this->width;
        this->y2 = this->y1 + this->height;

        this->x_mid = this->x1 + (this->width) / 2;
        this->y_mid = this->y1 + (this->height) / 2;
    }
    virtual void write_file(fstream& file){
       string result;
       result+="In Polygon:\n";
       result+="("+to_string(init_x1)+","+to_string(init_y1)+")";
       result+="("+to_string(init_x2)+","+to_string(init_y1)+")";
       result+="("+to_string(init_x2)+","+to_string(init_y2)+")";
       result+="("+to_string(init_x1)+","+to_string(init_y2)+")"+"\n";
       result+="Out Polygon:\n";
       result+="("+to_string(x1)+","+to_string(y1)+")";
       result+="("+to_string(x2)+","+to_string(y1)+")";
       result+="("+to_string(x2)+","+to_string(y2)+")";
       result+="("+to_string(x1)+","+to_string(y2)+")"+"\n";
       file<<result;
    }

    virtual int get_occu_area(){
        return area;
    }

};
